<?php 
try {
	include_once('db/constants.php');
	include_once('db/get_categories_db.php');
	include_once('db/error_log_db.php');
	include_once('db/update_user_db.php');
	include_once('facebook_login.php');

	$current_file = "Categories";
	include_once("users_flow_logger.php");

	updateUser($user_profile['id'], $user_profile['first_name'], $user_profile['last_name'], $_SERVER['HTTP_USER_AGENT']);
	
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

function getBests() {
	if ( isset($_REQUEST['get_bests']) ) { 
		if ($_REQUEST['get_bests'] == "1") {
			return 1;	
		}
		return 0;
	}

	return 1;
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" style="overflow-x: hidden">
<head>
	<meta property="fb:app_id" content="<?php echo APP_ID;?>" /> 
	<meta property="og:url" content="<?php echo APP_CANVAS;?>" /> 
	<meta property="og:title" content="<?php echo HTML_TITLE;?>" />
	<meta property="og:description" content="<?php echo DEFAULT_SITE_DESCRIPTION;?>" /> 
	<meta property="og:site_name" content="<?php echo SITE_NAME;?>" />
	<meta property="og:locale" content="es_LA" /> 
	<meta property="og:image" content="<?php echo SERVER_WEB;?>logo/logo.png" /> 

	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<title><?php echo HTML_TITLE;?></title>
	<link rel="stylesheet" href="css/style.css" type="text/css" media="all" />
	<!--[if lte IE 6]><link rel="stylesheet" href="css/ie6.css" type="text/css" media="all" /><![endif]-->
	
	<script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>
	<script src="js/spin.js" type="text/javascript"></script>
	<script src="js/general_slider_functions.js" type="text/javascript"></script>
	<script src="js/wrap_content.js?<?php echo REVISION;?>" type="text/javascript" content="text/html; charset=utf-8"></script>

	<script>
		APP_CANVAS = '<?php echo APP_CANVAS; ?>';
		var CONTENTS_URL = '<?php echo CONTENTS_URL; ?>';
		LOAD_AMOUNT = '<?php echo LOAD_AMOUNT; ?>';
		contents = JSON.parse( "<?php echo addslashes(get_bests_lasts_categories_db(INITIAL_LOAD_AMOUNT, -1, NULL_SCORE_VALUE, $user_profile['id'], getBests())); ?>" );
		allContentsLoaded = (contents.length < <?php echo INITIAL_LOAD_AMOUNT; ?>) ? true : false;
		getContentsUrlCall = "db/get_categories_db.php?limit=" + LOAD_AMOUNT + "&user_id=<?php echo $user_profile['id'] ?>";
		specialLastContent = false;
		bestById = false;
		var showingComments = false;

		// Header extra logger
		var buttonsHrefs = null;
		var buttonsToUpdate = new Array('header-first', 'header-second', 'header-categories', 'see-content-0', 'see-content-1', 'see-content-2', 'see-all-author-content-0', 'see-all-author-content-1', 'see-all-author-content-2', 'see-best-authors', 'see-best-contents');

		function updateHeaderHrefs() {
			if (buttonsHrefs == null) {
				buttonsHrefs = new Array();

				for (var i = 0; i < buttonsToUpdate.length; i++) {
					if (document.getElementById(buttonsToUpdate[i]))
						buttonsHrefs[i] = document.getElementById(buttonsToUpdate[i]).href;
				}
			}

			for (var i = 0; i < buttonsToUpdate.length; i++) {
				if (document.getElementById(buttonsToUpdate[i]))
					document.getElementById(buttonsToUpdate[i]).href = buttonsHrefs[i] + "&log_extra=" + generateExtraLog();
			}	
		}

		function updateLastIdContent() {
			if (contents.length > 0)
				lastIdContent = contents[ contents.length - 1 ].category_id;
		}

		function updateLastPointsContent() {
			if (contents.length > 0)
				lastPointsContent = contents[ contents.length - 1 ].score;
		}

		function showContent(category) {

			document.getElementById('category-name').innerHTML = removeSlashes(category.name);
			document.getElementById('enter-category').href = "<?php echo CONTENTS_URL; ?>" + "?category_id=" + category.category_id  + "&coming_from=<?php echo $current_file; ?>" + "&log_extra=" + generateExtraLog();
			document.getElementById("category-like").setAttribute("data-href", APP_CANVAS + CONTENTS_URL + "?category_id=" + category.category_id);
			
			// Check if FB is loaded
			if ("FB" in window)
				FB.XFBML.parse(document.getElementById('category-like-td'));
		}

		function showEmptyContents() {
			// Can't happend
			document.getElementById('category-name').innerHTML = '';
		}

	</script>
	
</head>
<body>

	<div id="fb-root"></div>
	<script>(function(d, s, id) {
	  var js, fjs = d.getElementsByTagName(s)[0];
	  if (d.getElementById(id)) return;
	  js = d.createElement(s); js.id = id;
	  js.src = "//connect.facebook.net/es_LA/all.js";
	  fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));</script>


    <script>		
	window.fbAsyncInit = function() {
	FB.init({
	  appId : '<?php echo APP_ID;?>',
	  xfbml : true,
	  version : 'v2.0'
	});
	FB.XFBML.parse(document.getElementById('category-like-td'));
	};
	(function(d, s, id){
         var js, fjs = d.getElementsByTagName(s)[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement(s); js.id = id;
         js.src = "//connect.facebook.net/es_LA/sdk.js";
         fjs.parentNode.insertBefore(js, fjs);
       }(document, 'script', 'facebook-jssdk'));
            
	function showLastContent() {

	}

	function updateComments() {
		// No comments at category page
	}

	function removeSlashes(str) {
		return str.replace(/\\/g, '');
	}

    </script>


<?php 
$headerFirstOption = MY_CATEGORIES;
$headerSecondOption = ADD_CATEGORY;
$headerThirdOption = HOME;

$headerFirstUrl = PUBLISH_CATEGORY."?coming_from=".$current_file;
$headerSecondUrl = UPLOAD_CATEGORY."?coming_from=".$current_file;
$headerThirdUrl = INDEX."?coming_from=".$current_file;

$headerFirstClass = "button";
$headerSecondClass = "button";
$headerThirdClass = "button";

$bestsHref = "#";
$lastsHref = "#";

$bestsClick = "showBestsContents()";
$lastsClick = "showLastsContents()";

include_once("header.php"); 
?>

<!-- Slider -->
<div id="slider">
	<div style="font-size: 25px; text-align: center; margin-top: 0px; padding-top: 17px; margin-bottom: -40px; color: #E8E8E8;">
		<?php echo CATEGORIES; ?>
	</div>
	<div class="shell">
		
		<div class="slider-holder">
			<div class="slides">			    	
				<!-- Slide -->
			    	<div class="slide-bg">
				    	<table border="0px" class="content-table">
				    		<tr>
				    			<td id="category-name" colspan="2" width="700px" class="category-td" align="center" valign="middle">
				    			
				    			</td>
				    		</tr>
				    		<tr style="height: 22%;">
				    			<td id="category-like-td" style="display: inline-block; width: 150px;">
									<div id="category-like" style="margin-top: 6px;" data-href="<?php echo APP_CANVAS; ?>" class="fb-like" data-width="150" data-layout="button_count" data-send="true" share="false" data-show-faces="false"></div>
				    			</td>
								<td align="right" valign="bottom" style="width: 62%;"> <a id="enter-category" style="margin-left: -27px;" class="big-button" href=""><?php echo ENTER_CATEGORY; ?></a> </td>
				    		</tr>
				    	</table>
			    	</div>
			    	<span class="slide-shadow">&nbsp;</span>
			    <!-- End Slide -->
			</div>
			
			<!-- Slider Navigation -->
			<div class="slider-nav">
				<a id="prev-button" href="#" onclick="moveLeft();" class="prev">previous</a>
				<a id="next-button" href="#" onclick="moveRight();" class="next">next</a>
			</div>
			<!-- End Slider Navigation -->
		</div>
		
	</div>
</div>
<!-- End Slider -->

<?php include_once('downOptions.php');?>

<div class="support-mail">
	<?php echo SUPPORT_MAIL_TEXT.SUPPORT_MAIL; ?>
</div>

<div id="facebook-comments" class="fb-comments-div" style="display: none;">
	<div id="fb-comments" class="fb-comments" data-href="http://example.com" data-width="700"></div>
</div>

<script> 

	var opts = {
		top: '30%', // Top position relative to parent
		left: '50%' // Left position relative to parent
	};
	var spinnerTarget = document.getElementById('slider');
	var spinner = new Spinner(opts).spin(spinnerTarget);

	hideMoveArrows();

	document.getElementById('category-name').innerHTML = '<img src="logo/LoadingLogo.png" style="margin-left: 95px; margin-top: -10px;" />';
	document.getElementById('enter-category').style.display = 'none';
	document.getElementById('category-like').style.display = 'none';
	
	window.onload = function() {
		spinner.stop();

		document.getElementById('enter-category').style.display = '';
		document.getElementById('category-like').style.display = '';

		// Content loaded from php
		showContentFromList(0);

		if (<?php echo getBests(); ?> == "1") {
			document.getElementById("bests-button").className = 'right-button-active';
			document.getElementById("lasts-button").className = 'left-button';
		} else {
			document.getElementById("bests-button").className = 'right-button';
			document.getElementById("lasts-button").className = 'left-button-active';
		}
	}

</script>

</body>
</html>
